home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 223_01 / fprintf.c < prev    next >
Text File  |  1980-01-01  |  3KB  |  80 lines

  1. #define NOCCARGC 
  2. /*
  3. ** Yes, that is correct.  Although these functions use an
  4. ** argument count, they do not call functions which need one.
  5. */
  6. #include stdio.h
  7.  
  8.  
  9. /*
  10. ** fprintf(fd, ctlstring, arg, arg, ...) - Formatted print.
  11. ** Operates as described by Kernighan & Ritchie.
  12. ** b, c, d, o, s, u, and x specifications are supported.
  13. ** Note: b (binary) is a non-standard extension.
  14. */
  15.  
  16. static int  arg, left, pad, cc, len, maxchr, width;
  17. static char *ctl, *sptr, str[17];
  18.  
  19. fprintf(argc) int argc; {
  20.   int *nxtarg;
  21.   nxtarg = CCARGC() + &argc;
  22.   return(Uprint(*(--nxtarg), --nxtarg));
  23.   }
  24.  
  25. /*
  26. ** printf(ctlstring, arg, arg, ...) - Formatted print.
  27. ** Operates as described by Kernighan & Ritchie.
  28. ** b, c, d, o, s, u, and x specifications are supported.
  29. ** Note: b (binary) is a non-standard extension.
  30. */
  31. printf(argc) int argc; {
  32.   return(Uprint(stdout, CCARGC() + &argc - 1));
  33.   }
  34.  
  35. /*
  36. ** Uprint(fd, ctlstring, arg, arg, ...)
  37. ** Called by fprintf() and printf().
  38. */
  39. Uprint(fd, nxtarg) int fd, *nxtarg; {
  40.   cc = 0;                                         
  41.   ctl = *nxtarg--;                          
  42.   while(*ctl) {
  43.     if(*ctl!='%') {fputc(*ctl++, fd); ++cc; continue;}
  44.     else ++ctl;
  45.     if(*ctl=='%') {fputc(*ctl++, fd); ++cc; continue;}
  46.     if(*ctl=='-') {left = 1; ++ctl;} else left = 0;       
  47.     if(*ctl=='0') pad = '0'; else pad = ' ';           
  48.     if(isdigit(*ctl)) {
  49.       width = atoi(ctl++);
  50.       while(isdigit(*ctl)) ++ctl;
  51.       }
  52.     else width = 0;
  53.     if(*ctl=='.') {            
  54.       maxchr = atoi(++ctl);
  55.       while(isdigit(*ctl)) ++ctl;
  56.       }
  57.     else maxchr = 0;
  58.     arg = *nxtarg--;
  59.     sptr = str;
  60.     switch(*ctl++) {
  61.       case 'c': str[0] = arg; str[1] = NULL; break;
  62.       case 's': sptr = arg;        break;
  63.       case 'd': itoa(arg,str);     break;
  64.       case 'b': itoab(arg,str,2);  break;
  65.       case 'o': itoab(arg,str,8);  break;
  66.       case 'u': itoab(arg,str,10); break;
  67.       case 'x': itoab(arg,str,16); break;
  68.       default:  return (cc);
  69.       }
  70.     len = strlen(sptr);
  71.     if(maxchr && maxchr<len) len = maxchr;
  72.     if(width>len) width = width - len; else width = 0; 
  73.     if(!left) while(width--) {fputc(pad,fd); ++cc;}
  74.     while(len--) {fputc(*sptr++,fd); ++cc; }
  75.     if(left) while(width--) {fputc(pad,fd); ++cc;}  
  76.     }
  77.   return(cc);
  78.   }
  79.  
  80.